/**
 * Jaxson ajax
 *
 * Copyright Remedy Interactive, licensed under Apache version 2 license:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0.html
 *
 * This notice must remain in all java source files.
 */
package org.jaxson.struts.charts.bar;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jaxson.struts.charts.ChartFormat;
import org.jfree.chart.labels.CategoryToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.urls.CategoryURLGenerator;
import org.jfree.data.category.CategoryDataset;

/**
 * Formatting specific to a Bar Chart or Stacked Bar Chart.
 * 
 * @author Joe Maisel
 *
 */
public class BarChartFormat extends ChartFormat
{
	private boolean isStacked;
	private boolean is3D;
	private String categoryLabel, valueLabel;
	private PlotOrientation orientation = PlotOrientation.VERTICAL;
	private Map<String, String> clickUrls = new HashMap<String, String>();
	private List<Color> colors = new ArrayList<Color>();
	
	private CategoryToolTipGenerator toolTipGenerator = new CategoryToolTipGenerator()
	{
		@Override
		public String generateToolTip( CategoryDataset pcd, int row, int col )
		{	
			//return String.format( getToolTipFormat(), pcd.getRowKey(row), pcd.getColumnKey(col), pcd.getValue( row, col ) );
			return pcd.getRowKey( row ) + " " + pcd.getColumnKey( col );
		}
	};
	
	private CategoryURLGenerator urlGenerator = new CategoryURLGenerator()
	{
		@Override
		@SuppressWarnings("unchecked")
		public String generateURL( CategoryDataset cds, int row, int col )
		{
			String result = "";
			
			if( getClickUrl( row, col ) != null )
				result = getClickUrl( row, col );

			if( getClickUrl( cds.getRowKey( row ), cds.getColumnKey( col ) ) != null )
				result = getClickUrl( cds.getRowKey( row ), cds.getColumnKey( col ) );
			
			return result;
		}
	};
	
	/**
	 * @return the colors
	 */
	public List<Color> getColors()
	{
		return colors;
	}

	/**
	 * @param colors
	 *            the colors to set
	 */
	public void setColors( List<Color> colors )
	{
		this.colors = colors;
	}

	/**
	 * Push a color
	 */
	public void addColor( Color c )
	{
		colors.add( c );
	}

	/**
	 * Set a color by index.
	 */
	public void setColor( int idx, Color c )
	{
		colors.set( idx, c );
	}
	
	/**
	 * @return the orientation
	 */
	public PlotOrientation getOrientation()
	{
		return orientation;
	}

	/**
	 * @param orientation the orientation to set
	 */
	public void setOrientation( PlotOrientation orientation )
	{
		this.orientation = orientation;
	}

	/**
	 * @return the categoryLabel
	 */
	public String getCategoryLabel()
	{
		return categoryLabel;
	}

	/**
	 * @param categoryLabel the categoryLabel to set
	 */
	public void setCategoryLabel( String categoryLabel )
	{
		this.categoryLabel = categoryLabel;
	}

	/**
	 * @return the valueLabel
	 */
	public String getValueLabel()
	{
		return valueLabel;
	}

	/**
	 * @param valueLabel the valueLabel to set
	 */
	public void setValueLabel( String valueLabel )
	{
		this.valueLabel = valueLabel;
	}

	/**
	 * @return the isStacked
	 */
	public boolean isStacked()
	{
		return isStacked;
	}

	/**
	 * @param isStacked the isStacked to set
	 */
	public void setStacked( boolean isStacked )
	{
		this.isStacked = isStacked;
	}

	/**
	 * Set the URL which will be called when clicking on a region of the chart.
	 */
	public void setClickUrl( int row, int col, String value )
	{
		clickUrls.put(  row + "." + col, value );
	}

	/**
	 * Set the URL which will be called when clicking on a region of the chart.
	 */
	public void setClickUrl( Comparable<? extends Object> rowKey, Comparable<? extends Object> colKey, String value )
	{
		clickUrls.put(  rowKey + "." + colKey, value );
	}
	
	/**
	 * Get the URL which will be called when clicking on a region of the chart.
	 */
	public String getClickUrl( int row, int col )
	{
		return clickUrls.get( row + "." + col );
	}
	
	/**
	 * Get the URL which will be called when clicking on a region of the chart.
	 */
	public String getClickUrl( Comparable<? extends Object> rowKey, Comparable<? extends Object> colKey )
	{
		return clickUrls.get( rowKey + "." + colKey );
	}
	
	/**
	 * @return the toolTipGenerator
	 */
	public CategoryToolTipGenerator getToolTipGenerator()
	{
		return toolTipGenerator;
	}

	/**
	 * @param toolTipGenerator the toolTipGenerator to set
	 */
	public void setToolTipGenerator( CategoryToolTipGenerator toolTipGenerator )
	{
		this.toolTipGenerator = toolTipGenerator;
	}

	/**
	 * @return the urlGenerator
	 */
	public CategoryURLGenerator getUrlGenerator()
	{
		return urlGenerator;
	}

	/**
	 * @param urlGenerator the urlGenerator to set
	 */
	public void setUrlGenerator( CategoryURLGenerator urlGenerator )
	{
		this.urlGenerator = urlGenerator;
	}

	/**
	 * @return the is3D
	 */
	public boolean isIs3D()
	{
		return is3D;
	}

	/**
	 * @param is3D the is3D to set
	 */
	public void setIs3D( boolean is3D )
	{
		this.is3D = is3D;
	}
}
